import breadcrumb from '@/components/common/Breadcrumb'
import fixedbutton from '@/components/common/fixedbutton'
import download from '@/components/common/download'
import {saveWin,delFile,findFileList} from '@/api/api.js'

export default {
    components:{
        breadcrumb,
        fixedbutton,
        download,
    },
    data() {
        return {
            brand:[
                {name:'lang.fun_exp'},
                {name:'cm.edit'},
            ],
            showCollapse:"1",
            hasIcon:false,
            fileList:[],
            fileListDownLoad:[],
            fileUploadUrl:process.env.UPDOWN_ROOT,
            showCollapse1:"1",
            fileProps:[ //文件上传的属性
                {prop:'fileRealName',label:'文件名'},
                {prop:'createTime',label:'上传时间'},
            ],
            win:{},
        }
    },
    methods: {
        //数据初始化
        initData:function(){
            this.win = this.$route.query.obj;
            // this.win.cudWinId = this.$route.query.obj.cudWinId;
            // this.win.cudWinLevel = this.$route.query.obj.cudWinLevel;
            // this.win.cudPrizeAmount = this.$route.query.obj.cudPrizeAmount;
            // this.win.cudWinName = this.$route.query.obj.cudWinName;
            // this.win.cudPhone = this.$route.query.obj.cudPhone;
            // this.win.cudWinSpeech = this.$route.query.obj.cudWinSpeech;
            // this.win.cudAwardDate = this.$route.query.obj.cudAwardDate;
            // this.win.cudWinDate = this.$route.query.obj.cudWinDate;
            // this.win.cudPayType = this.$route.query.obj.cudPayType;
            // this.win.cudBankCardNo = this.$route.query.obj.cudBankCardNo;
            if(this.$route.query.obj.cudBonusProperty != null){
                this.win.cudBonusProperty = this.$route.query.obj.cudBonusProperty.split(',');
            }else{
                this.win.cudBonusProperty = [];
            }
            
            let _this = this;
            findFileList({winId:this.$route.query.obj.cudWinId}).then((result) => {
                if(result.status == "200" && result.data.code == "0"){
                    _this.fileListDownLoad = result.data.data;
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
            
        },
        //取消操作
        back:function(){
            this.$router.push("/redemp_manage");
        },
        //保存兑奖
        save:function(){
            //判断获奖表
            this.$refs['win'].validate((valid)=>{
                if(valid){
                    //设置关联文件
                    this.win.fileList = this.fileListDownLoad;
                    this.win.cudBonusProperty = this.win.cudBonusProperty.join(',');
                    let _this = this;
                    //存储数据
                    saveWin(this.win).then((result) => {
                        if(result.status == "200" && result.data.code=="0"){
                            _this.$alert("保存成功!",_this.$t('cm.tips'),{
                                callback:_ => {
                                    _this.$router.push("/redemp_manage");
                                }
                            });
                        }else{
                            _this.$alert(result.data.msg,_this.$t('cm.tips'));
                        }
                    }).catch((err) => {
                        _this.$alert(err,_this.$t('cm.tips'));
                    });
                }else{
                    return false;
                }
            })  
        },
        //上传文件时的回调
        upfileBack:function(response, file, fileList){
            if(response.code == "0"){
                this.fileListDownLoad.push(response.data);
            }else{
                this.$alert(response.msg,this.$t('cm.tips'));
            }      
        },
        //删除文件
        delFiles:function(row){
            let _this = this;
            delFile({fileId:row.fileId}).then((result) => {
                if(result.status == "200" && result.data.code == "0"){
                    _this.fileListDownLoad.splice(_this.fileListDownLoad.findIndex(item => item.fileId===row.fileId),1);
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        }
    },
    mounted() {
        //初始化数据 加载列表数据 加载文件
        this.initData();
    },
    computed: {
        winRules:function(){
            return {
                cudWinLevel:[
                    {required:true,message:this.$t('fe.input_winLevel'),trigger:'change'}
                ],
                cudPrizeAmount:[
                    {required:true,message:this.$t('fe.input_prizeAmount'),trigger:'blur'}
                ],
                cudWinName:[
                    {required:true,message:this.$t('fe.input_winName'),trigger:'blur'}
                ],
                cudPhone:[
                    {required:true,message:this.$t('fe.input_phone'),trigger:'blur'}
                ],
                cudPayType:[
                    {required:true,message:this.$t('fe.input_cudPayType'),trigger:'change'}
                ],
                cudAwardDate:[
                    {required:true,message:this.$t('fe.input_cudAwardDate'),trigger:'change'}
                ],
                cudWinDate:[
                    {required:true,message:this.$t('fe.input_cudWinDate'),trigger:'change'}
                ]
                
            }
        }
    },
}